package com.grammer.thread;

import com.grammer.ThreadMainSubTest;

public class ThreadTest {
    private volatile int j = 0;

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest() ;
        AddRun addRun = threadTest.new AddRun();
        SubRun subRun = threadTest.new SubRun();

        for(int i=0;i<2;i++){
            Thread t = new Thread(addRun);
            t.start();
            t = new Thread(subRun);
            t.start();
        }
    }

    private synchronized void inc(){
        j++ ;
        System.out.println(Thread.currentThread().getName()+"-inc:"+j);
    }

    private synchronized void dec(){
        j--;
        System.out.println(Thread.currentThread().getName()+"-dec:"+j);
    }

    private class AddRun implements Runnable {
        @Override
        public void run() {
           for(int i=0;i<100;i++){
               inc();
           }
        }
    }

    private class SubRun implements Runnable {
        @Override
        public void run() {
            for(int i=0;i<100;i++){
                dec();
            }
        }
    }
}
